package com.atguigu.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.TravelGroupMapper;
import com.atguigu.entity.PageResult;
import com.atguigu.entity.QueryPageBean;
import com.atguigu.pojo.TravelGroup;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = TravelGroupService.class)

public class TravelGroupServiceImpl implements TravelGroupService {

    @Autowired
    private TravelGroupMapper travelGroupMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(Integer[] travelItemIds, TravelGroup travelGroup) {
        //1.先添加跟团游信息，同时返回数据表生成的主键
        travelGroupMapper.save(travelGroup);
        //2.批量添加
        travelGroupMapper.saveGroupIdsAndItemIds(travelGroup.getId(),travelItemIds);

    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        List<TravelGroup> list = travelGroupMapper.findAllByQuery(queryPageBean);
        PageInfo<TravelGroup> pageInfo = new PageInfo<TravelGroup>(list);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public void deleteById(Integer id) {
        travelGroupMapper.deleteById(id);
    }

    @Override
    public void edit(Integer[] travelItemIds, TravelGroup travelGroup) {
        travelGroupMapper.edit(travelGroup);//修改跟团游自身信息
        //修改跟团游和自由行的关联关系
        travelGroupMapper.deleteByGId(travelGroup.getId());

        travelGroupMapper.saveGroupIdsAndItemIds(travelGroup.getId(),travelItemIds);
    }

    @Override
    public TravelGroup findOne(Integer id) {
        return travelGroupMapper.findOne(id);
    }

    @Override
    public List<TravelGroup> findAll() {
        return travelGroupMapper.findAllByQuery(new QueryPageBean());
    }


}
